module.exports = (sequelize, DataTypes) => {
    const user = sequelize.import('./user.model')
    const consumer = sequelize.define('consumer', {
      uid: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      address: {
        type: DataTypes.STRING,
        allowNull: true
      },
    },{
        timestamps:false,
        tableName:'comsumer'
    })
    consumer.belongsTo(user, {
        foreignKey: 'uid',
        sourceKey: 'uid',
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
    })
    user.hasOne(consumer,{
        foreignKey: 'uid',
        sourceKey: 'uid',
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
    })
    // consumer.afterCreate(async (consumer) => {
    //   await user.create({
    //     uid: consumer.uid,
    //     identity:'用户',
    //   })
    // })
    consumer.beforeDestroy(async (consumer) => {
      await user.destroy({
        where: {
          uid: consumer.uid
        }
      })
    })
    return consumer
  }
